package com.keepmoving.util;


import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;


/**
 * @author gyy
 * @category 验证码工具包
 */
public class SendSms {
    @Test
    public void test() {
        System.out.println(ACCESSKEYID);
        System.out.println(ACCESSSECRET);
//        String code = getSendSms("15769392776");
//        System.out.println("code--->" + code);
    }

    @Value("${accessKeyId}")
    private String ACCESSKEYID;
    @Value("${accessSecret}")
    private String ACCESSSECRET;
    /**
     * @param phoneNunmber
     * @return 返回验证码值
     * @category 手机号接收
     */
    public String getSendSms(String phoneNunmber) {
        // AccessKey ID
        String accessKeyId = "LTAI4GCR8ZhtppAvi6ntmyvj";
        // accessSecret ms
        String accessSecret = "OnY71e88xXdwmFdOauJulxyvVdHiNs";
        // 验证码变量 str1、str2、str3、str4、str5、str6
        String str1 = (int) (Math.random() * 9) + "";
        String str2 = (int) (Math.random() * 9) + "";
        String str3 = (int) (Math.random() * 9) + "";
        String str4 = (int) (Math.random() * 9) + "";
        String str5 = (int) (Math.random() * 9) + "";
        String str6 = (int) (Math.random() * 9) + "";
        // 最终形成的验证码
        String code = str1 + str2 + str3 + str4 + str5 + str6;
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNunmber);
        request.putQueryParameter("SignName", "伊特Hitte餐饮");
        request.putQueryParameter("TemplateCode", "SMS_205065080");
        request.putQueryParameter("TemplateParam", "{\"code\":" + code + "}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getHttpStatus());
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return code;
    }
}
